//
//  MainTabbar.swift
//  微博222
//
//  Created by jessica on 15/11/8.
//  Copyright © 2015年 jessica. All rights reserved.
//

import UIKit

class MainTabbar: UITabBar {

   //布局
    //默认构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    //如果重写 init(frame:) 系统会默认为这个控件是通过代码创建
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        //如果有调用者通过 sb / xib创建了该对象 这个时候程序就崩溃
        setupUI()
    }
    
    private func setupUI (){
        
        //撰写添加按钮
        
        addSubview(composeBtn)
    }
    
    
    //重新布局
    override func layoutSubviews() {
        
        
        super.layoutSubviews()
    //手动修改"button"位置
        let w = self.bounds.width / 5
        
        let h = self.bounds.height
        
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        //递增变量
        //初始化记得 age 吗? 一样的
        var index : CGFloat = 0
        
        for subView  in subviews {
            
        //属于同一个类
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                //偏移量
            subView.frame = CGRectOffset(rect, index * w,  0)
                
                if index == 1 {
                
                index++
                
                }
                index++
            
            }
        }
      //设置撰写按钮的位置,下标为2 ,第三个
        composeBtn.frame = CGRectOffset(rect, 2 * w, 0)
        bringSubviewToFront(composeBtn)
        
        
    }
    
    //懒加载
    
    lazy var composeBtn : UIButton = {
    
    //设置按钮
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        
        return btn

    }()
    
    }
